<?php
namespace App\Controller;



use App\Model\CityModel;
use App\Model\ItemsCityModel;
use App\Model\ItemsModel;
use Think\Upload;

class ImportController extends BaseController
{

    //导入单品列表
    public function item(){

        $file_info = null;
        if (!empty($_FILES['file']['name'])) {
            $upload = $this->excelUploadObj();
            $file_info = $upload->uploadOne($_FILES['file']);
            if ($file_info === false) {
                $this->ajaxReturn(['error' => $upload->getError()]);
            }
        }

        $file = APP_PATH . '../Upload/' . $file_info['savepath'] . $file_info['savename'];

        if (!file_exists($file)) {

            $this->error("文件不存在");
            return false;
        }


        require __DIR__ . '/../../../ThinkPHP/Library/Vendor/PHPExcel-1.8.1/Classes/PHPExcel.php';

        $data=[];
        $batch = time();
        try {
            $reader = \PHPExcel_IOFactory::createReaderForFile($file);
            $excel = $reader->load($file);
            $sheet = $excel->getActiveSheet();
            $i = 4;
            $data =[];
            $now = date('Y-m-d H:i:s');
            while (true) {
                $key = ++$i;
                $name = $sheet->getCellByColumnAndRow(7, $key)->getValue();//判断是否跳出循环
                if(!$name){
                    break;
                }
                $info =[];
                $info=[
                    'c_operations'=>$this->IsObject($sheet->getCellByColumnAndRow(0, $key)->getValue()),
                    'key_code'=>$this->IsObject($sheet->getCellByColumnAndRow(1, $key)->getValue()),
                    'item_code'=>$this->IsObject($sheet->getCellByColumnAndRow(2, $key)->getValue()),
                    'sub_code'=>$this->IsObject($sheet->getCellByColumnAndRow(3, $key)->getValue()),
                    'item_key'=>$this->IsObject($sheet->getCellByColumnAndRow(4, $key)->getValue()),
                    'dept_code'=>$this->IsObject($sheet->getCellByColumnAndRow(5, $key)->getValue()),
                    'from_type'=>$this->IsObject($sheet->getCellByColumnAndRow(6, $key)->getValue()),
                    'item_name'=>$this->IsObject($sheet->getCellByColumnAndRow(7, $key)->getValue()),
                    'item_eng_name'=>$this->IsObject($sheet->getCellByColumnAndRow(8, $key)->getValue()),
                    'item_desc_on_dm'=>$this->IsObject($sheet->getCellByColumnAndRow(9, $key)->getValue()),
                    'info_on_dm'=>$this->IsObject($sheet->getCellByColumnAndRow(10, $key)->getValue()),
                    'unit'=>$this->IsObject($sheet->getCellByColumnAndRow(11, $key)->getValue()),
                    'characteristics'=>$this->IsObject($sheet->getCellByColumnAndRow(12, $key)->getValue()),
                    'nl'=>$this->IsObject($sheet->getCellByColumnAndRow(13, $key)->getValue()),
                    'nsp'=>$this->IsObject($sheet->getCellByColumnAndRow(14, $key)->getValue()),
                    'psp'=>$this->IsObject($sheet->getCellByColumnAndRow(15, $key)->getValue()),
                    'psp_start_date'=>$this->IsObject($sheet->getCellByColumnAndRow(16, $key)->getValue()),
                    'psp_end_date'=>$this->IsObject($sheet->getCellByColumnAndRow(17, $key)->getValue()),
                    'update_eng_name'=>$this->IsObject($sheet->getCellByColumnAndRow(18, $key)->getValue()),
                    'remark'=>$this->IsObject($sheet->getCellByColumnAndRow(19, $key)->getValue()),
                    'member_item_flag'=>$this->IsObject($sheet->getCellByColumnAndRow(20, $key)->getValue()),
                    'msp'=>$this->IsObject($sheet->getCellByColumnAndRow(21, $key)->getValue()),

                    'is_show'=>$this->IsObject($sheet->getCellByColumnAndRow(88, $key)->getValue()),
                    'topic_field'=>$this->IsObject($sheet->getCellByColumnAndRow(89, $key)->getValue()),
                    'topic_name'=>$this->IsObject($sheet->getCellByColumnAndRow(90, $key)->getValue()),
                    'index_recommend'=>$this->IsObject($sheet->getCellByColumnAndRow(91, $key)->getValue()),
                    'brand'=>$this->IsObject($sheet->getCellByColumnAndRow(92, $key)->getValue()),
                    'brand_item'=>$this->IsObject($sheet->getCellByColumnAndRow(93, $key)->getValue()),
                    'brand_recommend'=>$this->IsObject($sheet->getCellByColumnAndRow(94, $key)->getValue()),
                    'batch'=>$batch,
                    'create_at'=>$now
                ];
                //叠加ext_info
                $info['ext_info']=[];

                for($j =22;$j<55;$j++){
                    $info['ext_info'][]=[
                        //'city_code'=>$this->IsObject($sheet->getCellByColumnAndRow(93, $key)->getValue()),
                        //'solt_city_code'=>$this->IsObject($sheet->getCellByColumnAndRow($j, 4)->getValue()),
                        'solt_type'=>$this->IsObject($sheet->getCellByColumnAndRow($j, $key)->getValue()),
                        'page_city_code'=>$this->IsObject($sheet->getCellByColumnAndRow($j+33, 4)->getValue()),
                        'page_value'=>$this->IsObject($sheet->getCellByColumnAndRow($j+33, $key)->getValue()),
                    ];
                }


                $data[]=$info;
            }

        } catch (\Exception $e) {
            return false;
        }
       //插入

        $this->insert_code($data,$batch,$now);

    }


    private function insert_code( array $data,$batch,$now)
    {
        //$chunk_size = 200;
        //$arr = array_slice($data,0,$chunk_size);
        $arr = $data;
        foreach ($arr as $key=>$value){
            $city_list =$arr[$key]['ext_info'];
            unset($arr[$key]['ext_info']);
            //插入sql
            $item_id = ItemsModel::singleton()->add($arr[$key]);
            if($item_id){
                $insert_data =[];
                foreach ($city_list as $k=>$v){
                    if($v['page_value']){
                        $insert_data[]=[
                            'item_id'=>$item_id,
                            'city_code'=>$v['page_city_code'],
                            'page_value'=>$v['page_value'],
                            'slot_type'=>$v['solt_type'],
                            'batch'=>$batch,
                            'create_at'=>$now
                        ];
                    }
                }
                ItemsCityModel::singleton()->addAll($insert_data);
            }
        }

    }

    //如果返回object
    private function IsObject($value){
        if(is_object($value)){
            return $value->__toString();
        }
        return $value;
    }

    //导入城市列表
    public function city(){

        $file_info = null;
        if (!empty($_FILES['file']['name'])) {
            $upload = $this->excelUploadObj();
            $file_info = $upload->uploadOne($_FILES['file']);
            if ($file_info === false) {
                $this->ajaxReturn(['error' => $upload->getError()]);
            }
        }

        $file = APP_PATH . '../Upload/' . $file_info['savepath'] . $file_info['savename'];

        if (!file_exists($file)) {

            $this->error("文件不存在");
            return false;
        }

        require __DIR__ . '/../../../ThinkPHP/Library/Vendor/PHPExcel-1.8.1/Classes/PHPExcel.php';

        $reader = \PHPExcel_IOFactory::createReaderForFile($file);
        $excel = $reader->load($file);
        $sheet = $excel->getActiveSheet();
        $city = [];
        $i =27;
        while (true){
            $city[]=[
                'city_code'=>$this->IsObject($sheet->getCellByColumnAndRow(++$i, 3)->getValue()),
                'create_at'=>date('Y-m-d H:i:s'),
                'is_show'=>1
            ];
            if(60==$i){
                break;
            }
        }
        CityModel::singleton()->addAll($city);
        print_r($city);


    }

    private function excelUploadObj()
    {
        $upload = new Upload();
        $upload->maxSize = 2 * 1024 * 1024;
        $upload->exts = array('xlsx', 'xls');
        $upload->rootPath = './Upload/';
        return $upload;
    }
}